package com.zsh.wanandroid.ui.activity;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dxq.basic_ui_library.loadSirCallBack.EmptyCallback;
import com.dxq.basic_ui_library.loadSirCallBack.ErrorCallback;
import com.dxq.basic_ui_library.loadSirCallBack.LoadingCallback;
import com.kingja.loadsir.core.LoadSir;
import com.zsh.wanandroid.R;
import com.zsh.wanandroid.api.ApiService;
import com.zsh.wanandroid.base.BaseActivity;
import com.zsh.wanandroid.bean.RankResult;
import com.zsh.wanandroid.http.ApiRequest;
import com.zsh.wanandroid.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author zsh
 * @date 2020/3/12.
 * 功能：排名页面
 */

public class RankActivity extends BaseActivity {

    private ImageView ivBack;
    private TextView tvTitle;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView rvRank;

    private RankAdapter rankAdapter;
    private List<RankResult.Data.Datas> mDataList = new ArrayList<>();

    private static final int DEFAULT_PAGE = 1;
    private int currentPage = DEFAULT_PAGE;//默认加载第一页
    private int pageCount;
    private View loadItemView;

    @Override
    public void initPrams(Bundle prams) {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_rank;
    }

    @Override
    public void initView(View view) {
        ivBack = $(R.id.ivBack);
        tvTitle = $(R.id.tvTitle);
        swipeRefreshLayout = $(R.id.swipeRefreshLayout);
        rvRank = $(R.id.rvRank);
    }

    @Override
    public void setListener() {
        ivBack.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack://返回
                back();
                break;
        }
    }

    @Override
    public void doBusiness(Bundle savedInstanceState) {
        //设置标题
        tvTitle.setText("积分排行榜");
        loadItemView = UIUtils.getView(R.layout.load_item_recycleview);
        initRecyclerView();
        initSwipeRefreshLayout();
        initLoadSir();
        getRank();
    }


    private void initRecyclerView() {
        //添加Android自带的分割线
        rvRank.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        rankAdapter = new RankAdapter(R.layout.rank_item_recycleview, mDataList);
        rvRank.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rvRank.setAdapter(rankAdapter);
    }


    private void initSwipeRefreshLayout() {
        //下拉刷新
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = DEFAULT_PAGE;
                getRank();
            }
        });

        //上拉加载更多
        rankAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage++;
                getRank();
            }
        });
    }

    private void initLoadSir() {
        loadService = LoadSir.getDefault().register(swipeRefreshLayout, new com.kingja.loadsir.callback.Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                currentPage = DEFAULT_PAGE;
                getRank();
            }
        });
    }

    private void back() {
        this.finish();
    }

    /**
     * 网络获取排行榜
     */
    private void getRank() {
        ApiService apiService = ApiRequest.getInstance(self).create(ApiService.class);
        apiService.getRankList(currentPage).enqueue(new Callback<RankResult>() {
            @Override
            public void onResponse(Call<RankResult> call, Response<RankResult> response) {
                swipeRefreshLayout.setRefreshing(false);
                RankResult body = response.body();
                if (null == body) {
                    loadService.showCallback(EmptyCallback.class);
                    return;
                }
                RankResult.Data data = body.getData();
                if (null == data) {
                    loadService.showCallback(EmptyCallback.class);
                    return;
                }
                currentPage = data.getCurPage();
                pageCount = data.getPageCount();
                mDataList = data.getDatas();
                //根据加载数据显示相应的页面
                if (null == mDataList) {
                    loadService.showCallback(EmptyCallback.class);
                    return;
                }
                List<RankResult.Data.Datas> datas = data.getDatas();
                if (null != datas && datas.size() > 0) {
                    loadService.showSuccess();//成功
                    if (currentPage == DEFAULT_PAGE) {
                        rankAdapter.replaceData(mDataList);
                    } else {
                        rankAdapter.addData(mDataList);
                    }
                    if (currentPage < pageCount) {
                        rankAdapter.loadMoreComplete();
                    } else {
                        rankAdapter.loadMoreEnd();
                    }
                } else {
                    loadService.showCallback(EmptyCallback.class);
                }
            }

            @Override
            public void onFailure(Call<RankResult> call, Throwable t) {
                swipeRefreshLayout.setRefreshing(false);
                loadService.showCallback(ErrorCallback.class);
                currentPage = DEFAULT_PAGE;
            }
        });

    }

    private class RankAdapter extends BaseQuickAdapter<RankResult.Data.Datas, BaseViewHolder> {

        public RankAdapter(@LayoutRes int layoutResId, @Nullable List<RankResult.Data.Datas> data) {
            super(layoutResId, data);
        }

        public RankAdapter(@LayoutRes int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(BaseViewHolder helper, RankResult.Data.Datas item) {
            helper.setText(R.id.tvRank, item.getRank()+"");
            helper.setText(R.id.tvUsername, item.getUsername());
            helper.setText(R.id.tvCoinCount, item.getCoinCount()+"");
        }
    }
}
